package org.swidgets.sinputdata;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.Widget;

public class HistorableButton extends FocusPanel implements SourcesClickEvents
{
	public static int WAITING=0;
	public static int CLICKED=10;
	public static int MENU=20;
	public static int SELECTED=30;
	
	private int status = WAITING;
	private int timeout = 1000;
	private ClickListener listener=null;
	private Timer timer=null;
	
	private String[] history=null;
	private final ListBox lb = new ListBox();
	private PopupPanel popuppanel = new PopupPanel(true);
	private Button bottone;
	
	/*
	 * TIMER
	 */
	private class ClickTypeListener extends MouseListenerAdapter
	{
		public void onMouseDown(Widget sender, int x, int y) {	
			status = WAITING;
			if (history!=null && history.length>0)
			{
				timer=new Timer(){				
					public void run() {					
						status = MENU;	
						menu();
					}
				};
				timer.schedule(timeout);
			}
		}


		public void onMouseUp(Widget sender, int x, int y) {
			if (status==WAITING)
			{
				status = CLICKED;			
				timer.cancel();
				timer=null;
				notifyClick();
			}
		}		
	}
	
	/*
	 * COSTRUCTOR
	 */
	
	public HistorableButton(String txt,ClickListener listener,String[] history)
	{		
		super.addMouseListener(new ClickTypeListener());
		bottone = new Button(txt);
		add(bottone);
		bottone.setWidth("20px");
		popolaHistory(history);
		
		if (listener!=null)
			addClickListener(listener);
		
		lb.addChangeListener(new ChangeListener(){

			public void onChange(Widget sender) {
				status=SELECTED;
				popuppanel.hide();
				notifyClick();
			}
			
		});
				
		popuppanel.add(lb);
		
		
	}
	
	public HistorableButton(String txt,int timeout,ClickListener listener,String[] history)
	{
		this(txt,listener,history);
		this.timeout=timeout;
	}

	/*
	 * STATUS
	 */
	
	public int getStatus(){return status;}

	/*
	 * LISTENER
	 */

	public void addClickListener(ClickListener listener) {
		this.listener=listener;
	}


	public void removeClickListener(ClickListener listener) {
		this.listener=null;
	}
	
	private void notifyClick()
	{
		if (listener!=null)
			listener.onClick(this);
	}
	
	/*
	 *  MENU
	 */
	private void menu()
	{
		int left = getAbsoluteLeft();
        int down =  getAbsoluteTop()+getOffsetHeight();
        popuppanel.setPopupPosition(left, down);	
       
     	popuppanel.show();
     	
	}
	
	/*
	 * GESTIONE HISTORY
	 */
	public void popolaHistory(String[] history)
	{
		
		lb.clear();
		this.history=history;

		if (this.history!=null && this.history.length>0)
		{
			for (int i=0;i<history.length;i++)
				lb.addItem(history[i]);
			lb.setVisibleItemCount(history.length);
		}
			
	}
	
	public void setSelectedIndex(int index){
		lb.setSelectedIndex(index);
	}
	
	public int getSelectedIndex(){
		return lb.getSelectedIndex();
	}
	
	public int getItemCount(){
		return lb.getItemCount();
	}
	
	public void setFirstIndex(){
		lb.setSelectedIndex(0);	
	}
	
	public void setLastIndex(){
		lb.setSelectedIndex(lb.getItemCount()-1);
	}
	
	public void setStyleName(String style) {
		bottone.setStyleName(style);
	}
}

